package com.qut.graduate.busrtquery.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.core.PoiInfo;
import com.baidu.mapapi.utils.DistanceUtil;
import com.qut.graduate.busrtquery.R;
import com.qut.graduate.busrtquery.greendao.bean.BusLine;
import com.qut.graduate.busrtquery.greendao.bean.BusLineCollect;
import com.qut.graduate.busrtquery.greendao.bean.StationInfo;
import com.qut.graduate.busrtquery.greendao.bean.StationInfoCollect;
import com.qut.graduate.busrtquery.interfaces.OnItemClickListener;

import java.util.Collections;
import java.util.List;

public class StationInfoAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final String TAG = "StationInfoAdapter";
    private Context mContext;
    private List<?> mStationInfoList;
    private LatLng currentLatLng;
    private int tag;

    public StationInfoAdapter(Context context, List<?> stationInfoList,int tag) {
        this.mContext = context;
        this.tag = tag;
        this.mStationInfoList = stationInfoList;
    }

    @Override
    public int getItemViewType(int position) {
        return 0;
    }

    @Override
    public int getItemCount() {
        return mStationInfoList != null ? mStationInfoList.size() : 0;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_station_info, parent, false);
        return new StationInfoHolder(view);
    }

    @SuppressLint("DefaultLocale")
    @Override
    public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, int position) {
        if (mOnItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = holder.getLayoutPosition();
                    mOnItemClickListener.onClick(holder.itemView, position);
                }
            });
        }
        StationInfoHolder stationInfoHolder = (StationInfoHolder) holder;
        StationInfo stationInfo = null;
        if (tag == 0){
            stationInfo = (StationInfo) mStationInfoList.get(position);
        }if (tag == 1){
            stationInfo = new StationInfo((StationInfoCollect)mStationInfoList.get(position));
        }
        stationInfoHolder.mStationInfoName.setText(stationInfo.getName());
        if (stationInfo.getAddress().contains(";") && stationInfo.getAddress().contains("路"))
            stationInfoHolder.mStationInfoIcon.setImageResource(R.mipmap.icon_bus);
        else stationInfoHolder.mStationInfoIcon.setImageResource(R.mipmap.icon_poi_point);
//        if (getItemCount() - 1 > 0 && position == getItemCount() - 1)
//            stationInfoHolder.mBottomLine.setVisibility(View.GONE);
//        else
//            stationInfoHolder.mBottomLine.setVisibility(View.VISIBLE);
    }

    public class StationInfoHolder extends RecyclerView.ViewHolder {
        ImageView mStationInfoIcon;
        TextView mStationInfoName;
        View mBottomLine;

        public StationInfoHolder(View view) {
            super(view);
            mStationInfoIcon = view.findViewById(R.id.item_station_info_icon);
            mStationInfoName = view.findViewById(R.id.item_station_info_name);
            mBottomLine = view.findViewById(R.id.item_station_info_bottom_line);
        }
    }

    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener OnItemClickListener) {
        this.mOnItemClickListener = OnItemClickListener;
    }

}